package nat_detect

import (
	"encoding/json"
	"fmt"
	"math/rand"
	"time"
)

type RawPkt struct {
	Cmd    string
	Id     uint64
	Remote string
}

func (p *RawPkt) String() string {
	return fmt.Sprintf("%s %d %s", p.Cmd, p.Id, p.Remote)
}
func MakePktWithCmd(cmd string) ([]byte, uint64) {
	rand.Seed(time.Now().UnixNano())
	randInt := rand.Uint64()
	pkt := RawPkt{
		Cmd: cmd,
		Id:  randInt,
	}
	data, err := json.Marshal(pkt)
	if err != nil {
		return []byte{}, randInt
	}
	return data, randInt
}

func ParsePkt(d []byte) *RawPkt {
	var pkt RawPkt
	if json.Unmarshal(d, &pkt) != nil {
		return nil
	}
	return &pkt
}
